<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/1/20 0020
 * Time: 下午 18:33
 */

namespace Tsnews\Controller;
use Think\Controller;

class TestController extends Controller{
    public function getdepinfo(){
        $redis = linkredis();
        $corpid = C('CORPID');
        $secret = C('SECRET');
        $a_token = tsnewsgetaccesstoken($corpid,$secret);
        $url3 = "https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token={$a_token}&id=1";
        $deplist = json_decode(https_request($url3),true)['department'];
        $bm_arr = [];
        foreach($deplist as $k=>$v){
            foreach($deplist as $k1=>$v1){
                if($v1['parentid'] == $v['id'] ){
                    $bm_arr[$v['id']][] = $v1['id'];
                }
            }
        }
        $b = array_keys($bm_arr);
        //dump($b);
        foreach($deplist as $k=>$v){
            if(!in_array($v['id'],$b)){
                $bm_arr[$v['id']] = [];
            }
        }
        return ($bm_arr);//每个部门的子部门ID


    }
    public function getuserinfo(){
        $a = time();
        $redis = linkredis();
        $corpid = C('CORPID');
        $secret = C('SECRET');
        $a_token = tsnewsgetaccesstoken($corpid,$secret);
        $url2 = "https://qyapi.weixin.qq.com/cgi-bin/user/list?access_token={$a_token}&department_id=1&fetch_child=1&status=1";
        $userlist = json_decode(https_request($url2),true)['userlist'];
        $arr = [];
        foreach($userlist as $k=>$v){
            $arr[$v['userid']]['name'] = $v['name'];
            $arr[$v['userid']]['department'] = $v['department'];
            $arr[$v['userid']]['avatar'] = $v['avatar'].'64';
        }
        $deplist = $this->getdepinfo();//每个部门的子部门id
        //dump($arr);exit;
        $bb = [];
        foreach($arr as $k=>$v){
            foreach($v['department'] as $k1=>$v1){
                $arr[$k]['zbm_id'] .= implode(',',$deplist[$v1]).',';
                $bb[$v1][] = $k;
            }
            $arr[$k]['zbm_id'] = trim($arr[$k]['zbm_id'],',');
        }
        //dump($arr);//每个成员的子部门
        //dump($bb);exit;//每个部门的userID
        foreach($bb as $k=>$v){
            $bb[$k] = implode(',',$v);
        }
        $cc = array_keys($bb);
        foreach($deplist as $k=>$v){
            if(!in_array($k,$cc)){
                $bb[$k] = '';
            }
        }
        //dump($bb);exit;//每个部门的所有成员
        //dump($arr);exit;
        foreach($arr as $k=>$v){
            $_v = explode(',',$v['zbm_id']);
            foreach($_v as $k1=>$v1){
                if($bb[$v1]){
                    $arr[$k]['zbm_user'] .= $bb[$v1].',';
                }
            }
            $arr[$k]['zbm_user'] = implode(',',array_unique(explode(',',trim($arr[$k]['zbm_user'],','))));
        }
//        dump($bb);
//        dump($arr);//每个成员的详细信息
        $redis->set($corpid.'_dep_info',serialize($bb));
        $redis->expire($corpid.'_dep_info',3600*20);
        $redis->set($corpid.'_user_info',serialize($arr));
        $redis->expire($corpid.'_user_info',3600*20);
        echo (time()-$a);

    }
   public function aa(){
       $redis = linkredis();
       $redis->del('wxf5af4703e9956741_dep_info');
       $redis->del('wxf5af4703e9956741_user_info');
       $redis->del('tsnews_accesstoken');
//       $redis->del('depinfo');
//       $redis->del('tsnews_accesstoken');
//       $redis->del('wx354598b372f7261a_dep_user');
       dump($redis->keys('*'));
   }
}
